<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="bianhao">
        <el-input
          v-model="queryParams.bianhao"
          placeholder="请输入编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在库房" prop="repositoryId">
        <el-input
          v-model="queryParams.repositoryId"
          placeholder="请输入所在库房"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:cabinet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:cabinet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:cabinet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:cabinet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-divider><el-icon><star-filled /></el-icon><el-icon><star-filled /></el-icon><el-icon><star-filled /></el-icon></el-divider>
    <!-- 循环渲染cabinetList， 使用el-row el-col布局，每行显示5个el-card -->
     <el-row :gutter="20">
      <el-col :span="4" v-for="(item, index) in cabinetList" :key="index">
        <el-card style="max-width: 100%; margin-bottom: 20px;">
          <template #header>
            <div class="card-header">
              <span>{{ item.bianhao }}</span>
              <span>{{ item.name }}</span>
            </div>
          </template>
          <div class="card-body">
            <p><span>所在库房：</span><span>#{{ item.repositoryId }}号</span></p>
            <p><span>具体位置：</span><span>#{{ item.pai }}排 &nbsp;&nbsp; #{{ item.lie }}列</span></p>
            <p><span>节层数量：</span><span>{{ item.sectionCount }}节 &nbsp;&nbsp; {{ item.layerCount }}层</span></p>
            <p><span>总计容量：</span><span>{{ item.capacity }}</span></p>
            <p><span>已有档案数量：</span><span>{{ item.size }}</span></p>
            <p><span>累计打开次数：</span><span>100</span></p>
          </div>
          <template #footer>
            <div class="card-footer">
              <el-button type="primary" @click="handleDetail(item)">查看</el-button>
              <el-button type="primary" @click="handleUpdate(item)">修改</el-button>
              <el-button type="primary" @click="handleDelete(item)">删除</el-button>
            </div>
          </template>
        </el-card>
      </el-col>
     </el-row>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案柜管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cabinetRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="bianhao">
          <el-input v-model="form.bianhao" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="所在库房" prop="repositoryId">
          <el-input v-model="form.repositoryId" placeholder="请输入所在库房" />
        </el-form-item>
        <el-form-item label="排号" prop="pai">
          <el-input v-model="form.pai" placeholder="请输入排号" />
        </el-form-item>
        <el-form-item label="列号" prop="lie">
          <el-input v-model="form.lie" placeholder="请输入列号" />
        </el-form-item>
        <el-form-item label="节数" prop="sectionCount">
          <el-input v-model="form.sectionCount" placeholder="请输入节数" />
        </el-form-item>
        <el-form-item label="层数" prop="layerCount">
          <el-input v-model="form.layerCount" placeholder="请输入层数" />
        </el-form-item>
        <el-form-item label="容量" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入容量" />
        </el-form-item>
        <el-form-item label="已有档案数" prop="size">
          <el-input v-model="form.size" placeholder="请输入已有档案数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cabinet">
import { listCabinet, getCabinet, delCabinet, addCabinet, updateCabinet } from "@/api/manage/cabinet";
import { useRoute } from 'vue-router';

const { proxy } = getCurrentInstance();

const cabinetList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const route = useRoute();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    bianhao: null,
    name: null,
    repositoryId: null,
    sectionCount: null,
    layerCount: null,
    capacity: null,
    size: null,
  },
  rules: {
    bianhao: [
      { required: true, message: "编号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    repositoryId: [
      { required: true, message: "所在库房不能为空", trigger: "blur" }
    ],
    pai: [
      { required: true, message: "排号不能为空", trigger: "blur" }
    ],
    lie: [
      { required: true, message: "列号不能为空", trigger: "blur" }
    ],
    sectionCount: [
      { required: true, message: "节数不能为空", trigger: "blur" }
    ],
    layerCount: [
      { required: true, message: "层数不能为空", trigger: "blur" }
    ],
    capacity: [
      { required: true, message: "容量不能为空", trigger: "blur" }
    ],
    size: [
      { required: true, message: "已有档案数不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询档案柜管理列表 */
function getList() {
  loading.value = true;
  listCabinet(queryParams.value).then(response => {
    cabinetList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    bianhao: null,
    name: null,
    repositoryId: null,
    pai: null,
    lie: null,
    sectionCount: null,
    layerCount: null,
    capacity: null,
    size: null,
    temperature: null,
    moisture: null,
    oxygen: null,
    waterlogging: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("cabinetRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加档案柜管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCabinet(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改档案柜管理";
  });
}

/** 查看详情按钮操作 */
function handleDetail(row) {
  proxy.$router.push({ path: '/manage/cabinet/detail/', query: { id: row.id} });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cabinetRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCabinet(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCabinet(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除档案柜管理编号为"' + _ids + '"的数据项？').then(function() {
    return delCabinet(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/cabinet/export', {
    ...queryParams.value
  }, `cabinet_${new Date().getTime()}.xlsx`)
}


onMounted(() => {
  // 确保 route.query.id 存在并且是数字
  if (route.query.id) {
    queryParams.value.repositoryId = parseInt(route.query.id, 10);
  }
  getList();
});
</script>

<style scoped>
.card-header {
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-footer {
  padding: 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
